package messagesvc.executor.subcribe;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.ucloud.microsf.commonlib.response.PageListResponse;
import cn.ucloud.microsf.commonlib.util.PageUtil;
import cn.ucloud.microsf.messagesvc.orm.mapper.SubscribeMapper;
import cn.ucloud.microsf.messagesvc.orm.model.SubscribeDO;
import cn.ucloud.microsf.messagesvc.request.record.SubscribeListRequest;
import cn.ucloud.microsf.messagesvc.response.record.SubscribeListResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息模板
 */
@Slf4j
@Service
public class SubscribeListExecutor {

  @Autowired
  private SubscribeMapper subscribeMapper;

  public PageListResponse<SubscribeListResponse> execute(SubscribeListRequest request) {

    HashMap<String, String> req = new HashMap<>();
    req.put("sub_id", request.getSubId());
    req.put("sub_name", request.getSubName());

    IPage<SubscribeDO> iPage = new Page<>(PageUtil.getPage(request.getPage()), PageUtil.getPageSize(request.getPageSize()));
    long total = subscribeMapper.searchTotal(req);
    iPage.setTotal(total);
    if (total > 0) {
      List<SubscribeDO> records = subscribeMapper.searchPage(iPage.offset(), iPage.getSize(), req);
      iPage.setRecords(records);
    }

    List<SubscribeListResponse> result = new ArrayList<>();
    if (CollectionUtil.isNotEmpty(iPage.getRecords())) {
      for (SubscribeDO record : iPage.getRecords()) {
        SubscribeListResponse response = new SubscribeListResponse();
        BeanUtil.copyProperties(record, response);
        response.setStatusLabel(record.getStatus().getDesc());
        result.add(response);
      }
    }

    return PageListResponse.generateResponse(result, (int) iPage.getPages(), (int) iPage.getSize(), (int) iPage.getTotal());

  }

}

